# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2021/10/18 22:19
# @File           : db_base.py
# @IDE            : PyCharm
# @desc           : 数据库公共 ORM 模型
from datetime import datetime
from sqlmodel import SQLModel, Field
from typing import TypeVar, Generic, Optional

T = TypeVar("T")


class Result(SQLModel, Generic[T]):
    ok: bool = True
    code: int = 200
    message: Optional[str] = "OK"
    data: Optional[T]

    def __init__(self, data: Optional[T] = None, **kwargs):
        super().__init__(data=data, **kwargs)


class TimestampMixin:
    create_datetime: Optional[datetime] = Field(
        description="创建时间", default_factory=datetime.now()
    )
    update_datetime: Optional[datetime] = Field(
        description="更新时间", default_factory=datetime.now()
    )
    delete_datetime: Optional[datetime] = Field(
        description="删除时间", default_factory=datetime.now()
    )


class BaseEntity(SQLModel, TimestampMixin):
    """
    公共 ORM 模型，基表
    """

    # id: int | None = Field(default=None, primary_key=True)
    id: Optional[int] = Field(default=None, primary_key=True, description="主键ID")
    is_delete: bool = Field(default=False, description="是否软删除")
